<?php

namespace mini;

class Paginate
{
    public int $totalRows;
    public int $eachPage;
    public int $maxPage;
    public int $currentPage = 1;
    public string $limit;
    public $firstPage;
    public $prevPage;
    public array $listPage = [];
    public $nextPage;
    public $lastPage;
    public $skipPage;
    public string $currentUrl;

    public function __construct(int $totalRows, int $eachPage = 10)
    {
        // if ($totalRows < 1) {
        //     throw new \InvalidArgumentException("总行数必须为正整数。");
        // }
        if ($eachPage <= 0) {
            throw new \InvalidArgumentException("每一页都必须是一个正整数。");
        }

        $this->totalRows = $totalRows;
        $this->eachPage  = $eachPage;
        $this->maxPage = ceil($this->totalRows / $this->eachPage);
        $this->currentPage = max(1, (int)PAGE_NUMBER);
        $this->currentUrl = '/' . (URL_SEGMENT ?: METHOD_NAME);

        $suffix = '/';
        $this->limit     = ' limit ' . (($this->currentPage - 1) * $eachPage) . ',' . $eachPage;
        // 抽象URL生成
        $getsRec         = $this->addGet();
        $this->firstPage = $this->generateUrl(1);
        $this->prevPage = $this->currentPage <= 1 ? '' : $this->generateUrl($this->currentPage - 1);
        $this->nextPage = $this->currentPage >= $this->maxPage ? '' : $this->generateUrl($this->currentPage + 1);
        $this->lastPage = $this->generateUrl($this->maxPage);

        // 分页列表
        if ($this->currentPage <= 3) {
            $start = 1;
            $end   = 6;
        } else {
            $start = $this->currentPage - 2;
            $end   = $this->currentPage + 3;
        }

        if ($end > $this->maxPage) {
            $end = $this->maxPage;
        }

        if ($end - $start < 5) {
            $start = $end - 5;
        }

        if ($start < 1) {
            $start = 1;
        }

        for ($i = $start; $i <= $end; $i++) {
            $this->listPage[$i] = $this->currentUrl . '/p' . $i . $suffix . $getsRec;
        }

        // 跳转分页
        $this->skipPage = '<select onchange="location.href=\'' . $this->currentUrl . '/p\'+this.value+\'' . $suffix . $getsRec . '\';">';
        for ($i = 1; $i <= $this->maxPage; $i++) {
            if ($i == $this->currentPage) {
                $this->skipPage .= '<option value="' . $i . '" selected="selected">' . $i . '</option>';
            } else {
                $this->skipPage .= '<option value="' . $i . '">' . $i . '</option>';
            }
        }
        $this->skipPage .= '</select>';
    }

    public function pager()
    {
        return array($this->firstPage, $this->prevPage, $this->listPage, $this->nextPage, $this->lastPage);
    }

    public function skipPager()
    {
        return $this->skipPage;
    }

    private function generateUrl(int $page): string
    {
        $suffix = '/';
        $getsRec = $this->addGet();
        return $this->currentUrl . '/p' . $page . $suffix . $getsRec;
    }

    public function addGet()
    {
        // 过滤$_GET数组，只保留标量值
        $filteredGet = array_filter($_GET, function ($value, $key) {
            return is_scalar($value) && !is_bool($value);
        }, ARRAY_FILTER_USE_BOTH);

        // 使用http_build_query()构建查询字符串
        $queryString = http_build_query($filteredGet);

        // 如果查询字符串为空，则返回空字符串
        if (empty($queryString)) {
            return '';
        }

        return '?' . $queryString;
    }
}
